package com.sykj.bsc.rwl.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sykj.bsc.rwl.po.SysDept;
import com.sykj.bsc.rwl.po.SysRole;
import com.sykj.bsc.rwl.po.SysUser;
import com.sykj.bsc.rwl.service.SysDeptService;

@Controller
@RequestMapping("sysDept")
public class SysDeptController {
	
	@Autowired
	private SysDeptService sysDeptService;
	
	
	/**
	 * @Title: selectSysDept   
	 * @Description: 动态遍历组织结构树状   
	 * @param: @param model
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("selectAllSysDept")
	public  String selectAllSysDept(Model model,HttpServletRequest req) throws Exception{
		SysDept sysDept=getSysDept(req);
		HashMap<String, String> map=new HashMap<String,String>();
		map.put("cascade_id",sysDept.getCascade_id());
		String data=sysDeptService.selectAllSysDept(map);
		model.addAttribute("msg", data);
		model.addAttribute("path","/bsc/sysDept/selectSysDept");
		return "backstage/rwl/menu";
	}
	
	private SysDept getSysDept(HttpServletRequest req) throws Exception{
		SysUser sysuser= (SysUser) req.getSession().getAttribute("user");
		int dept_id=sysuser.getDept_id();
		SysDept sysDept=sysDeptService.findByIdSysDept(dept_id);
		return sysDept;
	}
	
	@RequestMapping("selectSysDept")
	public String selectSysDept(Model model,HttpServletRequest req) throws Exception {
		HashMap<String,String> map=mapinitMap(req);
		List<SysDept> list=sysDeptService.selectSysDept(map);
		model.addAttribute("list", list);
		return "backstage/rwl/sysDept";
	}
	
	/**
	 * @throws Exception 
	 * @Title: mapinitMap   
	 * @Description: 初始化map用了查询  
	 * @param: @param req
	 * @param: @return      
	 * @return: HashMap<Object,Object>      
	 * @throws
	 */
	private HashMap<String,String> mapinitMap(HttpServletRequest req) throws Exception {
		HashMap<String,String> map=new HashMap<>();
		String cascade_id=req.getParameter("cascade_id");
		String dept_name=req.getParameter("dept_name");
		SysDept sysDept=getSysDept(req);
		if(sysDept!=null) {
			map.put("a", sysDept.getCascade_id());
		}
		if(dept_name!=null && !dept_name.equals("")) {
			map.put("dept_name", dept_name);
			req.setAttribute("dept_name", dept_name);
		}
		if(cascade_id!=null && !cascade_id.equals("")) {
			map.put("cascade_id", cascade_id);
			req.setAttribute("cascade_id", cascade_id);
		}
		return map;
	}
	
	/**
	 * @Title: saveSysDept   
	 * @Description: 修改和保存
	 * @param: @param sysDept
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("saveSysDept")
	public String saveSysDept(SysDept sysDept,HttpServletRequest req) throws Exception {
		HttpSession session=req.getSession();
		SysUser su=(SysUser) session.getAttribute("user");
		String user_id=su.getUser_id()+"";
		if(sysDept.getDept_id2().equals("0")) {
			sysDept.setCreate_user_id(user_id);
			sysDeptService.insertSysDept(sysDept);
		}else {
			sysDept.setModify_user_id(user_id);
			sysDeptService.updateSysDept(sysDept);
		}
		return "redirect:selectAllSysDept";
	}
	
	/**
	 * @Title: deleteSysDept   
	 * @Description: T通过id删除组织  
	 * @param: @param dept_id
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("deleteSysDept")
	public String deleteSysDept(@RequestParam("dept_id")Integer dept_id) throws Exception {
		sysDeptService.deleteSysDept(dept_id);
		return "redirect:selectAllSysDept";
	}
	
	/**
	 * @Title: updateSysDept   
	 * @Description: 通过id查询出来后返回到jsp页面  
	 * @param: @param dept_id
	 * @param: @param resp
	 * @param: @throws Exception      
	 * @return: void      
	 * @throws
	 */
	@RequestMapping("updateSysDept")
	public void updateSysDept(@RequestParam("dept_id")Integer dept_id,HttpServletResponse resp) throws Exception {
		SysDept sysDept=sysDeptService.findByIdSysDept(dept_id);
		SysUserController.getWirte(resp, sysDept);
	} 
}
